package com.xsp.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsp.commonutils.R;
import com.xsp.statistics.client.UcenterClient;
import com.xsp.statistics.entity.StatisticsDaily;
import com.xsp.statistics.mapper.StatisticsDailyMapper;
import com.xsp.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author xsp
 * @since 2022-03-04
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    UcenterClient ucenterClient;

    //统计某天注册人数，生成统计数据
    @Override
    public void registerCount(String day) {
        //如果重复统计的话，会有重复的数据，因此我们需要删除原来的数据后才能新增数据(也可以更新原来的数据)
        //删除某天存在的数据(旧数据)
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        //远程调用拿到某一天注册人数
        R r = ucenterClient.registerCount(day);
        //拿到返回值，返回的数据是Integer类型的
        Integer countByDay = (Integer)r.getData().get("countByDay");

        //把获取的数据添加到数据库的统计分析表中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);//需要统计的日期
        statisticsDaily.setRegisterNum(countByDay);//某日的注册人数
        statisticsDaily.setCourseNum(RandomUtils.nextInt(1,20));//某日新增课程数(1-20的随机int值) TODO
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));//某日登录人数(100-200的随机int值) TODO
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));//某日播放视频数 TODO

        //放入统计数据(最新数据)
        baseMapper.insert(statisticsDaily);

    }

    //获取图表统计数据
    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        //只取type类型的列和date_calculated列
        queryWrapper.select(type,"date_calculated");
        //时间范围筛选
        queryWrapper.between("date_calculated",begin,end);
        //获取查到的列表数据
        List<StatisticsDaily> chartList = baseMapper.selectList(queryWrapper);

        //保存x轴的数据(日期)
        List<String> xChart = new ArrayList<>();
        //保存y轴的数据(对应的type)
        List<Integer> yChart = new ArrayList<>();

        //拿到列表中每个对象的数据放入x,y轴List中
        for (StatisticsDaily s:chartList) {
            //x轴的日期
            xChart.add(s.getDateCalculated());
            //不同的type拿到不同的值
            switch (type){
                case "register_num":
                    yChart.add(s.getRegisterNum());
                    break;
                case "login_num":
                    yChart.add(s.getLoginNum());
                    break;
                case "video_view_num":
                    yChart.add(s.getVideoViewNum());
                    break;
                case "course_num":
                    yChart.add(s.getCourseNum());
                    break;
            }
        }
        //xy轴数据放入map中
        Map<String,Object> map = new HashMap<>();
        map.put("xChart",xChart);
        map.put("yChart",yChart);

        return map;
    }
}
